package org.agridata.akvisualization.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 */
public class DataUtil {

    /**
     * 获取当天的 0 点时间
     * @return
     */
    public static String startTime(){
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        // 将时分秒设置为0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        // 获取当天0点的时间戳
        long startTimestamp = calendar.getTimeInMillis();
        // 将时间戳格式化为指定的日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startDate = dateFormat.format(new Date(startTimestamp));
        return startDate;
    }

    /**
     * 获取当天的 23点59分59秒 时间
     * @return
     */
    public static String endTime(){
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        // 将时分秒设置为23:59:59
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        // 获取当天23:59:59的时间戳
        long endTimestamp = calendar.getTimeInMillis();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endDate = dateFormat.format(new Date(endTimestamp));
        return endDate;
    }

    /**
     * 获取昨天的 0点 时间
     * @return
     */
    public static String startOfDay(){
        //获取昨天的日期
        LocalDateTime yesterday = LocalDateTime.now().minusDays(1);

        //设置时间为00:00:00
        LocalDateTime startOfDay = yesterday.withHour(0).withMinute(0).withSecond(0);

        //使用指定的格式将日期格式化为字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startOfDayString = startOfDay.format(formatter);

        return startOfDayString;
    }

    /**
     * 获取昨天的 23点59分59秒 时间
     * @return
     */
    public static String endOfDay(){
        //获取昨天的日期
        LocalDateTime yesterday = LocalDateTime.now().minusDays(1);

        //设置时间为23:59:59
        LocalDateTime endOfDay = yesterday.withHour(23).withMinute(59).withSecond(59);

        //使用指定的格式将日期格式化为字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String endOfDayString = endOfDay.format(formatter);

        return endOfDayString;
    }



    public static final String DATE_FORMAT = "yyyy-MM-dd";

    public static String getYesterday(String dateString) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        Date date = sdf.parse(dateString);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date yesterday = calendar.getTime();
        return sdf.format(yesterday);
    }


    public static String getLastYearDate(String dateString ) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        Date date = sdf.parse(dateString);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, -1);
        Date lastYearDate = calendar.getTime();
        return sdf.format(lastYearDate);
    }






}
